package com.dis.service.service.impl;

import com.dis.pojo.entity.Reimbursement;
import com.dis.service.mapper.MedicalCardMapper;
import com.dis.service.mapper.ReimbursementMapper;
import com.dis.pojo.queryDTO.ReimbursementPageQueryDTO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.dis.common.context.BaseContext;
import com.dis.pojo.dto.ReimbursementDTO;
import com.dis.pojo.entity.MedicalCard;
import com.dis.common.result.PageResult;
import com.dis.service.service.ReimbursementService;
import com.dis.pojo.vo.ReimbursementVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

@Service
@Slf4j
public class ReimbursementServiceImpl implements ReimbursementService {

    @Autowired
    private ReimbursementMapper reimbursementMapper;

    @Autowired
    private MedicalCardMapper medicalCardMapper;


    @Override
    public void save(ReimbursementDTO reimbursementDTO) {
        // 根据医疗卡id查询医院信息
        MedicalCard medicalCard = medicalCardMapper.getByMedicalCardId(reimbursementDTO.getMedicalCardId());
        Reimbursement reimbursement = new Reimbursement();
        BeanUtils.copyProperties(reimbursementDTO, reimbursement);
        reimbursement.setHospitalId(medicalCard.getHospitalId());
        reimbursement.setCreateTime(LocalDateTime.now());
        reimbursement.setUpdateTime(LocalDateTime.now());
        reimbursement.setIsDeleted(0);
        // 自动生成一条订单
        reimbursement.setInvoiceNumber("INV" + System.currentTimeMillis());
        reimbursement.setStatus(0); // 新增时状态为待审核
        reimbursement.setIsRemit(0); // 新增时汇款状态为未汇款
        reimbursementMapper.insert(reimbursement);
    }

    @Override
    public PageResult pageQuery(ReimbursementPageQueryDTO reimbursementPageQueryDTO) {
        // 使用PageHelper进行分页
        PageHelper.startPage(reimbursementPageQueryDTO.getPage(), reimbursementPageQueryDTO.getPageSize());
        
        // 调用Mapper查询数据
        Page<ReimbursementVO> page = (Page<ReimbursementVO>) reimbursementMapper.pageQuery(reimbursementPageQueryDTO);
        
        // 返回分页结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public ReimbursementVO getById(Long id) {
        ReimbursementVO reimbursementVO = reimbursementMapper.getById(id);
        return reimbursementVO;
    }

    @Override
    public void deleteById(Long id) {
        reimbursementMapper.deleteById(id);
    }

    @Override
    public void audit(Long id, Integer status, String auditComment) {
        Reimbursement reimbursement = new Reimbursement();
        reimbursement.setId(id);
        reimbursement.setStatus(status);
        reimbursement.setAuditComment(auditComment);
        reimbursement.setAuditUserId(BaseContext.getCurrentId());
        reimbursement.setAuditDate(LocalDateTime.now());
        reimbursement.setUpdateTime(LocalDateTime.now());
        reimbursementMapper.update(reimbursement);
    }
}